﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using SeleniumToolkit.Util;


namespace SeleniumToolkit.Install
{
    [RunInstaller(true)]
    public class SeleniumToolsRegistryInstaller : Installer
    {
        private const string RegistryKeyName = @"SOFTWARE\SeleniumToolkit";
        private const string InstallDir = "InstallDir";
        private const string ServerFolder = "server";

        public SeleniumToolsRegistryInstaller()
        {
#if DEBUG
            System.Diagnostics.Debugger.Launch();
#endif
        }

        public override void Install(IDictionary stateSaver)
        {
            // create key if not present
            RegistryKeyHelper helper = new RegistryKeyHelper(RegistryKeyName, true);

            helper.SetValue(InstallDir, TargetDir);

            base.Install(stateSaver);
        }

        public override void Uninstall(IDictionary savedState)
        {
            // delete key 
            RegistryKeyHelper helper = new RegistryKeyHelper(RegistryKeyName);
            if (helper.RegistryKeyExists)
            {
                helper.Delete();
            }
            base.Uninstall(savedState);
        }

        /*
        protected string SeleniumServerDirectory
        {
            get
            {
                return System.IO.Path.Combine(TargetDir, ServerFolder);
            }
        }
        */

        protected string TargetDir
        {
            get
            {
                string item = this.Context.Parameters["targetdir"];
                if (item == null)
                {
                    // TODO: Missing InstallerArgumentException
                    throw new ArgumentNullException("targetdir","Missing targetdir parameter.");
                }
                return item;
            }
        }
    }
}
